import request from "@/utils/system/requestMy";
import { AxiosRequestConfig, Method } from "axios";

//post
export function postAction(
  url: string,
  data?: any,
  config?: AxiosRequestConfig
) {
  let params = {
    url: url,
    method: "POST",
    data,
  };
  if (config) {
    // Object.assign(params, config);
    return request(Object.assign(params, config));
  } else {
    return request({
      url: url,
      method: "POST",
      data,
    });
  }
}

//post method= {post | put}
export function httpAction(url: string, parameter: object, method: Method) {
  // let params = {
  //   url,
  //   method,
  //   data: parameter,
  // };
  return request({
    url,
    method,
    data: parameter,
  });
}

//put
export function putAction(url: string, parameter: object) {
  return request({
    url: url,
    method: "put",
    data: parameter,
  });
}

//get
export function getAction(
  url: string,
  parameter?: object,
  config?: AxiosRequestConfig
) {
  let params = {
    url: url,
    method: "get",
    params: parameter,
  };
  if (config) {
    let paramsList = Object.assign(params, config);
    return request(Object.assign({ url }, paramsList));
  } else {
    return request({
      url: url,
      method: "get",
      params: parameter,
    });
  }
}

//deleteAction
export function deleteAction(url: string, parameter?: object) {
  return request({
    url: url,
    method: "delete",
    params: parameter,
  });
}

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url: string, parameter: object) {
  return request({
    url: url,
    params: parameter,
    method: "get",
    responseType: "blob",
  });
}
